package com.sunshuo.springsecuritydemo.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Properties;

/**
 * @ClassName CaptchaConfig
 * @Description 验证码配置文件
 * @Author sunshuo
 * @Date 2021/11/7 15:25
 */
@Configuration
@PropertySource(value = "classpath:captcha.properties")
public class CaptchaConfig {

    /**
     * 验证码边框
     */
    @Value("${kaptcha.border}")
    private String border;

    /**
     * 验证码边框颜色
     */
    @Value("${kaptcha.border.color}")
    private String borderColor;

    /**
     * 宽度
     */
    @Value("${kaptcha.image.width}")
    private String imageWidth;

    /**
     * 高度
     */
    @Value("${kaptcha.image.height}")
    private String imageHeight;

    /**
     * session key
     */
    @Value("${kaptcha.session.key}")
    private String sessionKey;

    /**
     * 验证码长度
     */
    @Value("${kaptcha.textproducer.char.length}")
    private String charLenght;

    /**
     * 字体颜色
     */
    @Value("${kaptcha.textproducer.font.color}")
    private String fontColor;

    /**
     * 字体大小
     */
    @Value("${kaptcha.textproducer.font.size}")
    private String fontSize;

    /**
     * 字体名称
     */
    @Value("${kaptcha.textproducer.font.names}")
    private String fontNames;

    /**
     * 验证码生成器Bean
     * @return
     */
    @Bean(name = "verifyCodeProducer")
    public DefaultKaptcha createVerifyCode() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", border);
        properties.setProperty("kaptcha.border.color", borderColor);
        properties.setProperty("kaptcha.image.width", imageWidth);
        properties.setProperty("kaptcha.image.height", imageHeight);
        properties.setProperty("kaptcha.session.key", sessionKey);
        properties.setProperty("kaptcha.textproducer.char.length", charLenght);
        properties.setProperty("kaptcha.textproducer.font.color", fontColor);
        properties.setProperty("kaptcha.textproducer.font.size", fontSize);
        properties.setProperty("kaptcha.textproducer.font.names", fontNames);
        defaultKaptcha.setConfig(new Config(properties));
        return defaultKaptcha;
    }

}
